package com.party.service.study.impl;

import com.party.common.Page;
import com.party.mapper.CurriculumMapper;
import com.party.model.Curriculum;
import com.party.model.CurriculumExample;
import com.party.model.User2Test;
import com.party.service.study.CurriculumService;
import com.party.service.study.User2TestService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class CurriculumServiceImpl implements CurriculumService
{
    @Resource
    CurriculumMapper curriculumMapper;
    @Resource
    User2TestService user2TestService;
    @Override
    public int add(Curriculum curriculum) {
        if(curriculum != null)
        {
            return this.curriculumMapper.insert(curriculum);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if(id != null && id > 0)
        {
            return this.curriculumMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public Curriculum getById(Integer id) {
        if(id != null && id > 0)
        {
            return this.curriculumMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public List<Curriculum> getByIds(List<Integer> ids) {
        if(ids != null && ids.size() > 0)
        {
            CurriculumExample example = new CurriculumExample();
            CurriculumExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(ids);
            return this.curriculumMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<Curriculum> getByUid(Integer uid) {
        if(uid != null && uid > 0)
        {
            return this.curriculumMapper.getListByUid(uid);
        }
        return null;
    }

    @Override
    public List<Curriculum> getByUidNew(Integer uid) {
        if(uid != null && uid > 0)
        {
            List<Curriculum> ret = new ArrayList<Curriculum>();
            List<Curriculum> myList = getByUid(uid);
            if(myList != null && myList.size()>0)
            {
                for(Curriculum curriculum:myList)
                {
                    List<User2Test> utList = user2TestService.getByCurrId(curriculum.getId());
                    //没查到说明没考试
                    if((utList == null || utList.size() <= 0) && curriculum.getIsfinished().equals("1"))
                    {
                        ret.add(curriculum);
                    }
                }
            }
            return ret;
        }
        return null;
    }

    @Override
    public List<Curriculum> getByUidOld(Integer uid) {
        if(uid != null && uid > 0)
        {
            List<Curriculum> ret = new ArrayList<Curriculum>();
            List<Curriculum> myList = getByUid(uid);
            if(myList != null && myList.size()>0)
            {
                for(Curriculum curriculum:myList)
                {
                    List<User2Test> utList = user2TestService.getByCurrId(curriculum.getId());
                    //查到说明已经考试过了
                    if(utList != null && utList.size() > 0 && curriculum.getIsfinished().equals("1"))
                    {
                        ret.add(curriculum);
                    }
                }
            }
            return ret;
        }
        return null;
    }

    @Override
    public Page getList(Curriculum curriculum, int pageSize, int pageNumber) {

        CurriculumExample example = new CurriculumExample();
        if(curriculum != null)
        {
            CurriculumExample.Criteria criteria = example.createCriteria();
            if(curriculum.getTitle() != null && !"".equals(curriculum.getTitle()))
            {
                criteria.andTitleLike("%"+curriculum.getTitle()+"%");
            }
            if(curriculum.getContent() != null && !"".equals(curriculum.getContent()))
            {
                criteria.andContentLike("%"+curriculum.getContent()+"%");
            }
        }
        //获取总的记录数量
        int count = this.curriculumMapper.countByExample(example);
        //获取指定的页码数据
        example.setBeginNum((pageNumber - 1) * pageSize);
        example.setEndNum(pageSize);
        List<Curriculum> list = this.curriculumMapper.getListPage(example);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;

    }

    @Override
    public int edit(Curriculum curriculum) {
        if(curriculum != null && curriculum.getId() != null && curriculum.getId() > 0)
        {
            return this.curriculumMapper.updateByPrimaryKeySelective(curriculum);
        }
        return 0;
    }

    @Override
    public List<Curriculum> getAll() {
        return this.curriculumMapper.selectByExample(new CurriculumExample());
    }

    @Override
    public List<Curriculum> getByContentId(Integer id) {
        if(id != null && id > 0)
        {
            return this.curriculumMapper.getListByContentId(id);
        }
        return null;
    }


}
